package com.person.tbsystem.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.person.tbsystem.R;
import com.person.tbsystem.fragment.FragmentAt;
import com.person.tbsystem.fragment.FragmentAuth;
import com.person.tbsystem.fragment.FragmentSpace;

/**
 * Created by zhezhetoo on 2015/12/27.
 */
public class MainActivity extends FragmentActivity implements View.OnClickListener {
    private FragmentAt fragmentAt;
    private FragmentAuth fragmentAuth;
    private FragmentSpace fragmentSpace;
    private FrameLayout atFl, authFl, spaceFl;
    private ImageView atIv, authIv, spaceIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        clickAtBtn();
    }

    private void initView() {
        atFl = (FrameLayout) findViewById(R.id.layout_at);
        authFl = (FrameLayout) findViewById(R.id.layout_auth);
        spaceFl = (FrameLayout) findViewById(R.id.layout_space);
        atIv = (ImageView) findViewById(R.id.image_at);
        authIv = (ImageView) findViewById(R.id.image_space);
        spaceIv = (ImageView) findViewById(R.id.image_space);
    }

    private void initData() {
        atFl.setOnClickListener(this);
        authFl.setOnClickListener(this);
        spaceFl.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_at:
                clickAtBtn();
                break;
            case R.id.layout_auth:
                clickAuthBtn();
                break;
            case R.id.layout_space:
                clickSpaceBtn();
                break;
        }
    }

    private void clickAtBtn() {
        fragmentAt = new FragmentAt();
        FragmentTransaction fragmentTransaction = this
                .getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.frame_content, fragmentAt);
        fragmentTransaction.commit();
        atFl.setSelected(true);
        atIv.setSelected(true);

        authFl.setSelected(false);
        authIv.setSelected(false);

        spaceFl.setSelected(false);
        spaceIv.setSelected(false);

    }

    private void clickAuthBtn() {
        fragmentAuth = new FragmentAuth();
        FragmentTransaction fragmentTransaction = this
                .getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.frame_content, fragmentAuth);
        fragmentTransaction.commit();

        atFl.setSelected(false);
        atIv.setSelected(false);

        authFl.setSelected(true);
        authIv.setSelected(true);

        spaceFl.setSelected(false);
        spaceIv.setSelected(false);
    }


    private void clickSpaceBtn() {
        fragmentSpace = new FragmentSpace();
        FragmentTransaction fragmentTransaction = this
                .getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.frame_content, fragmentSpace);
        fragmentTransaction.commit();

        atFl.setSelected(false);
        atIv.setSelected(false);

        authFl.setSelected(false);
        authIv.setSelected(false);

        spaceFl.setSelected(true);
        spaceIv.setSelected(true);
    }

}
